<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 载入编辑器抽像类
 */
require_once('Abstract.php');

/**
 * FCKeditor 封装
 *
 * @package    helper
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_View_Helper_Editor_FCK extends EGP_View_Helper_Editor_Abstract
{

    /**
     * 标识是否已经载入 JS 文件
     *
     * @var boolean
     */
    protected static $_isLoadedJS = false;

    /**
     * 风格样式
     *
     * @var string
     */
    protected $_style = 'Simple'; // Default/Custom/Simple/Basic

    /**
     * 编辑器根路径
     *
     * @var string
     */
    protected $_basePath = '/editor/fckeditor/';

    /**
     * 设置其他配置
     *
     * @var array
     */
    protected $_config = array(
        'ToolbarStartExpanded' => false, //是否在启动时展开编辑器
    );

    /**
     * 设定编辑器配置
     *
     * @see    http://www.imeelee.com/editor/fckeditor/fckconfig.js
     * @param  array  $config
     * @return EGP_View_Helper_Editor_FCK
     */
    public function setConfig(array $config)
    {
        $this->_config = $config;
        return $this;
    }

    /**
     * 输出 HTML 代码
     *
     * @return string
     */
    public function __toString()
    {
        $configStr = '';
        foreach ($this->_config as $key => $value) {
            if (is_bool($value) || strtolower($value) == 'true' || strtolower($value) == 'false') {
                $value = $value ? 'true' : 'false';
            } else {
                $value = var_export($value, true);
            }
            $configStr .= "\noFCKeditor.Config['$key'] = $value;";
        }
        $html = <<<EOF
<textarea id="{$this->_name}" name="{$this->_name}" style="display:none">{$this->_value}</textarea>
<script type="text/javascript" language="javascript">
var oFCKeditor = new FCKeditor('{$this->_name}', '{$this->_width}', '{$this->_height}', '{$this->_style}');
oFCKeditor.BasePath = '{$this->_basePath}';{$configStr}
oFCKeditor.ReplaceTextarea();
</script>
EOF;
        if (self::$_isLoadedJS === false) {
            $html = <<<EOF
<script type="text/javascript" language="javascript" src="{$this->_basePath}fckeditor.js"></script>
$html
EOF;
            self::$_isLoadedJS = true;
        }
        return $html;
    }

}